//
//  XQPublicIdentityAccurePopCell.swift
//  Spark Peso
//
//  Created by 小琦 on 2025/2/7.
//

import UIKit

class XQPublicIdentityAccurePopCell: UITableViewCell,UITextFieldDelegate {
    
    var textFielHandle: ((String) -> Void)?
    
    var btnHandle:(() -> Void)?

    private lazy var titleLb : UILabel = {
        let lb = UILabel()
        lb.textColor = .xq_black
        lb.font = UIFont.systemFont(ofSize: 14)
        return lb
    }()
    
    private lazy var bgView : UIView = {
        let view = UIView()
        view.layer.cornerRadius = 23
        view.backgroundColor = UIColor(hex: "#F6F6F6")
        return view
    }()
    
    private lazy var textField : UITextField = {
        let textField = UITextField()
        textField.returnKeyType = .done
        textField.font = UIFont.systemFont(ofSize: 16, weight: .medium)
        textField.delegate = self
        textField.textColor = .xq_black
        textField.keyboardType = .asciiCapable
        textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
        return textField
    }()
    
    private lazy var arrowImageView : UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "public_accure_arrow")
        imageView.sizeToFit()
        imageView.isHidden = true
        imageView.setContentHuggingPriority(.required, for: .horizontal)
        imageView.setContentCompressionResistancePriority(.required, for: .horizontal)
        return imageView
    }()
    
    private lazy var btn : UIButton = {
        let btn = UIButton(type: .custom)
        btn.isHidden = true
        btn.addTarget(self, action: #selector(buttonClick), for: .touchUpInside)
        return btn
    }()
    
    var name : String? = "" {
        didSet {
            self.titleLb.text = name
        }
    }
    
    var content : String? = "" {
        didSet {
            self.textField.text = content
        }
    }
    
    var showArrow : Bool = false {
        didSet {
            self.arrowImageView.isHidden = !showArrow
            self.btn.isHidden = !showArrow
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
       super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        makeUI()
        makeConstraints()
    }

    required init?(coder: NSCoder) {
       fatalError("init(coder:) has not been implemented")
    }
    
    private func makeUI() {
        self.backgroundColor = UIColor.clear
        self.contentView.backgroundColor = .clear
        self.contentView.addSubview(self.titleLb)
        self.contentView.addSubview(self.bgView)
        self.bgView.addSubview(self.textField)
        self.bgView.addSubview(self.arrowImageView)
        self.bgView.addSubview(self.btn)
    }
    
    private func makeConstraints() {
        self.titleLb.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(17)
        }
        self.bgView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLb.snp.bottom).offset(10)
            make.left.equalTo(17)
            make.right.equalTo(-17)
            make.height.equalTo(46)
            make.bottom.equalTo(-14)
        }
        self.arrowImageView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(-16)
        }
        self.textField.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalTo(16)
            make.right.equalTo(self.arrowImageView.snp.left).offset(-12)
        }
        self.btn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }

    @objc private func textFieldDidChange(_ textField: UITextField) {
        textFielHandle?(textField.text ?? "")
    }
    
    @objc private func buttonClick() {
        btnHandle?()
    }
}
